package com.qianbei.view.transport;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianbei.R;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * 作者:GaoQian on 2016/10/29.
 * 邮箱:gaoqian_zz@163.com
 */

public class PointAdapter extends android.widget.BaseAdapter {
    Context context;
    List<TrasportBean.ArrayBean.CustomerAryBean> pointlist;
    //存储空箱扫描是否被保存过的标记 。需求来源：目前的界面是【空箱装卸】和【重箱装卸】是并列显示的，希望修改成先显示【空箱装卸】完成空箱任务后再出现【重箱装卸】。
//    SharedPreferences pref = context.getSharedPreferences("isOver",0);
//    SharedPreferences.Editor editor =  pref.edit();
    public PointAdapter(Context context, List<TrasportBean.ArrayBean.CustomerAryBean> pointlist) {
        this.context = context;
        this.pointlist = shortList(pointlist);

    }

    private List<TrasportBean.ArrayBean.CustomerAryBean> shortList( List<TrasportBean.ArrayBean.CustomerAryBean> pointlist){
        if(pointlist.size()<2){
            return pointlist;
        }
        Collections.sort(pointlist, new Comparator<TrasportBean.ArrayBean.CustomerAryBean>() {
            public int compare(TrasportBean.ArrayBean.CustomerAryBean arg0, TrasportBean.ArrayBean.CustomerAryBean arg1) {

                int hits0 = Integer.parseInt(arg0.getStatus());
                int hits1 = Integer.parseInt(arg1.getStatus());
                if (hits1 < hits0) {
                    return 1;
                } else if (hits1 == hits0) {
                    return 0;
                } else {
                    return -1;
                }
            }
        });
        return pointlist;
    }

    @Override
    public int getCount() {
        return pointlist.size();

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.kh_income_item, null);
            viewHolder = new ViewHolder();
            viewHolder.ts_company = (TextView) convertView.findViewById(R.id.ts_company);
            viewHolder.ts_address = (TextView) convertView.findViewById(R.id.ts_address);
            viewHolder.contactPerson = (TextView) convertView.findViewById(R.id.contactPerson);
            viewHolder.contactPhone = (TextView) convertView.findViewById(R.id.contactPhone);
            viewHolder.ischeck = (TextView) convertView.findViewById(R.id.ischeck);
            viewHolder.start = (TextView) convertView.findViewById(R.id.start);
            viewHolder.rlInfo = (RelativeLayout) convertView.findViewById(R.id.customer_info);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.ts_company.setText(pointlist.get(position).getCustomerName());
        viewHolder.ts_address.setText(pointlist.get(position).getAddress());
        viewHolder.contactPerson.setText(pointlist.get(position).getContactPerson());
        viewHolder.contactPhone.setText(pointlist.get(position).getContactPhone());




//        if( pointlist.get(position).getStatus().equals("1")){
//            viewHolder.ischeck.setText("已收运");
//            viewHolder.start.setVisibility(View.GONE);
//        }else {
//            viewHolder.ischeck.setText("未收运");
//            viewHolder.start.setVisibility(View.VISIBLE);
//        }

        if( pointlist.get(position).getStatus().equals("0")){
            viewHolder.ischeck.setText("未收运");
            viewHolder.start.setVisibility(View.VISIBLE);
            viewHolder.start.setText("开始收运");
        }else if (pointlist.get(position).getStatus().equals("1")){
            viewHolder.ischeck.setText("收运中");
            viewHolder.start.setText("继续收运");
            viewHolder.start.setVisibility(View.VISIBLE);
        }else if (pointlist.get(position).getStatus().equals("2")){
            viewHolder.ischeck.setText("收运完成");
            viewHolder.start.setVisibility(View.GONE);
        }

        viewHolder.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i  = new Intent(context,RecordweightActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("key",pointlist.get(position));
                i.putExtras(mBundle);
                context.startActivity(i);
//                ( (BaseActivity)context).startActivity( RecordweightActivity.class);
            }
        });
        viewHolder.rlInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TransportDetailActivity.class);
                intent.putExtra("key",pointlist.get(position));
                context.startActivity(intent);
            }
        });


//        String dateStrToString = DateUtil.timeStamp2Date(String.valueOf(pointlist.get(position).getAddtime()), "");
//        viewHolder.mItemIncomeTime.setText(dateStrToString);


        return convertView;
    }


    class ViewHolder {
        TextView ts_company;
        TextView ts_address;
        TextView contactPerson;
        TextView contactPhone;
        TextView ischeck;
        TextView start;
        RelativeLayout rlInfo;

    }

}
